﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace AerolineaApp.Models
{
    public class AerolineaBD : DbContext
    {
        public DbSet<Vuelo> Vuelos { get; set; }
        public DbSet<Avion> Aviones { get; set; }
        public DbSet<Aerolinea> Aerolineas { get; set; }
        public DbSet<Pais> Paises { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Avion>().ToTable("Avion");
            modelBuilder.Entity<Vuelo>().ToTable("Vuelo");
            modelBuilder.Entity<Aerolinea>().ToTable("Aerolinea");
            modelBuilder.Entity<Pais>().ToTable("Pais");

            modelBuilder.Entity<Avion>().HasRequired(a => a.Aerolinea)
                                        .WithMany(b => b.Aviones)
                                        .HasForeignKey(a => a.AerolineaId)
                                        .WillCascadeOnDelete(false);

            modelBuilder.Entity<Vuelo>().HasRequired(v => v.Aerolinea)
                            .WithMany(a => a.Vuelos)
                            .HasForeignKey(v => v.AerolineaId)
                            .WillCascadeOnDelete(false);

            modelBuilder.Entity<Vuelo>().HasRequired(v => v.Avion)
                         .WithMany(a => a.Vuelos)
                         .HasForeignKey(v => v.AvionId)
                         .WillCascadeOnDelete(false);

            modelBuilder.Entity<Aerolinea>().HasRequired(a => a.Pais)
                        .WithMany(p => p.Aerolineas)
                        .HasForeignKey(a => a.PaisId)
                        .WillCascadeOnDelete(false);
        }
    }
}